package me.zhengjie.ship.shipsoftversion.service.impl;

import me.zhengjie.ship.shipsoftversion.domain.ShipSoftVersion;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.ship.shipsoftversion.repository.ShipSoftVersionRepository;
import me.zhengjie.ship.shipsoftversion.service.ShipSoftVersionService;
import me.zhengjie.ship.shipsoftversion.service.dto.ShipSoftVersionDto;
import me.zhengjie.ship.shipsoftversion.service.dto.ShipSoftVersionQueryCriteria;
import me.zhengjie.ship.shipsoftversion.service.mapper.ShipSoftVersionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author liwh
* @date 2020-04-22
*/
@Service
//@CacheConfig(cacheNames = "shipSoftVersion")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShipSoftVersionServiceImpl implements ShipSoftVersionService {

    private final ShipSoftVersionRepository shipSoftVersionRepository;

    private final ShipSoftVersionMapper shipSoftVersionMapper;

    public ShipSoftVersionServiceImpl(ShipSoftVersionRepository shipSoftVersionRepository, ShipSoftVersionMapper shipSoftVersionMapper) {
        this.shipSoftVersionRepository = shipSoftVersionRepository;
        this.shipSoftVersionMapper = shipSoftVersionMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ShipSoftVersionQueryCriteria criteria, Pageable pageable){
        Page<ShipSoftVersion> page = shipSoftVersionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(shipSoftVersionMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ShipSoftVersionDto> queryAll(ShipSoftVersionQueryCriteria criteria){
        return shipSoftVersionMapper.toDto(shipSoftVersionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ShipSoftVersionDto findById(Integer id) {
        ShipSoftVersion shipSoftVersion = shipSoftVersionRepository.findById(id).orElseGet(ShipSoftVersion::new);
        ValidationUtil.isNull(shipSoftVersion.getId(),"ShipSoftVersion","id",id);
        return shipSoftVersionMapper.toDto(shipSoftVersion);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ShipSoftVersionDto create(ShipSoftVersion resources) {
        return shipSoftVersionMapper.toDto(shipSoftVersionRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ShipSoftVersion resources) {
        ShipSoftVersion shipSoftVersion = shipSoftVersionRepository.findById(resources.getId()).orElseGet(ShipSoftVersion::new);
        ValidationUtil.isNull( shipSoftVersion.getId(),"ShipSoftVersion","id",resources.getId());
        shipSoftVersion.copy(resources);
        shipSoftVersionRepository.save(shipSoftVersion);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            shipSoftVersionRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ShipSoftVersionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShipSoftVersionDto shipSoftVersion : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("舷号", shipSoftVersion.getShipNo());
            map.put("部署系统1型号", shipSoftVersion.getSystemType1());
            map.put("部署系统2型号", shipSoftVersion.getSystemType2());
            map.put("部署系统1版本", shipSoftVersion.getSystemVersion1());
            map.put("部署系统2版本", shipSoftVersion.getSystemVersion2());
            map.put("开始时间", shipSoftVersion.getStartTime());
            map.put("结束时间", shipSoftVersion.getEndTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}